package com.echo.tagparse;

import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.TreeMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class TermFrequency {
	
	TreeMap myTreeMap = null;
	
	public TermFrequency()throws IOException{
		FileInputStream fis = null;
		BufferedInputStream bis = null;
		DataInputStream dis = null;
		try{
			File temp = new File(this.getClass().getResource("/").getPath()+"\\com\\echo\\res\\reviewFile.txt");
			fis = new FileInputStream(temp);
			bis = new BufferedInputStream(fis);
			dis = new DataInputStream(bis);
			StringBuffer sb = new StringBuffer();
			myTreeMap = new TreeMap();
			
			while(dis.available()!=0){
				sb =  new StringBuffer(dis.readLine());
				Pattern expression = Pattern.compile("[a-zA-Z]+");
				Matcher matcher = expression.matcher(sb.toString());
				
				Object word = null;
				while(matcher.find()){
					word = matcher.group();
					if(myTreeMap.containsKey(word)){
						Integer count = (Integer)myTreeMap.get(word);
						myTreeMap.put(word, new Integer(count.intValue()+1));
					}else{
						myTreeMap.put(word, new Integer(1));
					}
				}
//				System.out.println(sb.toString());
			}
			
		}finally{
			fis.close();
			bis.close();
			dis.close();
		}
	}
	
	public void showStuff(){
		Iterator iter = myTreeMap.keySet().iterator();
		Object key = null;
		while(iter.hasNext()){
			key = iter.next();
			System.out.println((String)key+": "+myTreeMap.get(key));
		}
	}
	
	public static void main(String[] args){
		try{
			TermFrequency tf = new TermFrequency();
			tf.showStuff();
		}catch(IOException e){
			e.printStackTrace();
		}

	}
}
